// index.js
import {
  get,
  post,
  put
} from '../../utils/request'
import {
  APIS
} from '../../utils/api'
import { timestampToTime } from '../../utils/util'
// 获取应用实例
const app = getApp()

Page({
  data: {
    showPop: false,
    info: {},
    details: [],
    title: [
      ['充值日期', '充值人', '联系方式', '充值金额', '会员余额'],
      ['领取日期', '领取人', '联系方式', '优惠券', '发放时间'],
    ],
    keys: [
      ['date', 'name', 'tel', 'pay', 'amount'],
      ['date', 'name', 'tel', 'alias', 'date']
    ],
    type: '',
    baseUrl: ''
  },
  onLoad(options) {
    const { type, id } = options;
    console.log(type, id)
    const baseUrl = type === '0'
      ? `${APIS.recharge}/${id}`
      : `${APIS.discount}/${id}`;
    this.setData({
      type,
      baseUrl
    })
    
    get(baseUrl).then(res => {
      let {data, details} = res.data;
      this.setData({
        details,
        info: {
          ...data,
          title: `充值${data.amount}元 送${data.presentation}元`,
          time: `${data.start} ~ ${data.end}`
        }
      });
    })
  },
  handleClose() {
    this.setData({
      showPop: false
    })
  },
  handleShow() {
    this.setData({
      showPop: true
    })
  },
  handleDel() {
    put(this.data.baseUrl + '/delete').then(res => {
      if (res.code === 0) {
        wx.navigateBack(-1);
      }
      else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }
    })
  }
})